package com.zhongkai.web.control.book;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import com.zhongkai.json.JSONObject;
import com.zhongkai.model.book.TDjCcdjxx;
import com.zhongkai.model.book.TDjCcywcl;
import com.zhongkai.service.book.MoveinService;
import com.zhongkai.tools.CheckWord;

/**
 * 页面控制单元，用于读取页面信息，进行数据查询，返回数据等操作
 * @author 胡宇峰
 *
 */
@Controller
public class Moveincontrol {
	
	@Autowired
	private MoveinService moveinservice;

	/******************************
	 * 如果是GET方式进入的，就显示界面，输出一些必要的信息
	 * @return
	 */
	@RequestMapping(value="/system/book/movein.do",method = RequestMethod.GET) 
	public String displayform()throws Exception
	{
		
		return "redirect:/system/book/moveinform.jsp";
	}
	
	/*********************************
	 * 如果是POST方式进入，代表有数据要保存进数据库
	 * 在事件完成后，再返回原界面 。
	 * @param tdjccdjxx
	 * @param tdjccywcl
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/system/book/movein.do",method = RequestMethod.POST)
	public String save(TDjCcdjxx tdjccdjxx,TDjCcywcl tdjccywcl,HttpServletRequest request,HttpServletResponse response) throws Exception{
		PrintWriter out=response.getWriter();
		if(moveinservice.save(tdjccdjxx, tdjccywcl,request))
			out.write(CheckWord.showMsg("车船登记信息保存成功！","/system/book/moveinform.jsp",request));
		else
			out.write(CheckWord.showMsg("输入信息有误！","/system/book/moveinform.jsp",request));
		return null;//"redirect:/system/book/moveinform.jsp";
		
	}
	/**
	 * 字符串转日期的代码
	 * @param binder
	 */
	
	@InitBinder
	public void initBinder(WebDataBinder binder){
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  
	    dateFormat.setLenient(false);  
	    binder.registerCustomEditor(Date.class, new CustomDateEditor(  
	            dateFormat, false)); 
	}
}
